package com.com.corejavalearnings.reflection;

import java.lang.reflect.Field;

/**
 * Created by pankaj on 7/30/15.
 */
public class GenericsWithReflection {

    public static void main(String[] args) {
        Loader<Model> loader = new Loader<>(Model.class);
        Model model = loader.load();
        System.out.println(model);
    }
}
class Loader<T>
{
    Class<T> clazz;
    T t;

    Loader() {
    }

    Loader(Class<T> clazz) {
        this.clazz = clazz;
    }

    protected T load()
    {
        try {
            t = clazz.newInstance();
            for(Field field : t.getClass().getFields())
            {
                System.out.print(field.getName());
                System.out.println("->"+field.get(t));
                field.set(t, "mysql");
                System.out.print(field.getName());
                System.out.println("->"+field.get(t));
            }
            return t;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }
}

class Model {
    public String dbName;
    public String driver;
    public String port;
    public String username;
    public String password;
    public String sql;

    @Override
    public String toString() {
        return "Model{" +
                "dbName='" + dbName + '\'' +
                ", driver='" + driver + '\'' +
                ", port='" + port + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", sql='" + sql + '\'' +
                '}';
    }
}
